<?
	include("../includes/main_inc.php");
	include("../../functions.inc.php");
	
	if($_GET['SFrom']) $SFrom=$_GET['SFrom'];
	elseif ($_POST['SFrom']) $SFrom=$_POST['SFrom'];
	if($_GET['SText']) $SText=$_GET['SText'];
	elseif ($_POST['STo']) $STo=$_POST['STo'];
	if($_GET['STo']) $STo=$_GET['STo'];
	elseif ($_POST['SText']) $SText=$_POST['SText'];
	if($_GET['SType']) $SType=$_GET['SType'];
	elseif ($_POST['SType']) $SType=$_POST['SType'];
	if($_GET['RType']) $RType=$_GET['RType'];
	elseif ($_POST['RType']) $RType=$_POST['RType'];
	else $RType="All";
	if($_GET['OType']) $OType=$_GET['OType'];
	elseif ($_POST['OType']) $OType=$_POST['OType'];
	else $OType="guestName";
	if($_GET['Page']) $Page=$_GET['Page'];
	elseif($_POST['Page']) $Page=$_POST['Page'];
	else $Page=1;
	
	$RecordsPerPage=10;
	
	if($_POST['Update']){
		if(is_array($_POST['bookingIDs'])){
			foreach($_POST['bookingIDs'] as $bookingID){
				$TSQL="INSERT INTO remittance (bookingsID,RDate) VALUES ($bookingID,NOW())";
				$r->RawQuery($TSQL);
				print $TSQL;
			}
		}
	}
	
	if($RType!="All"){
		if($RType=="Paid"){
			$SearchSQL.=" AND RDate IS NOT NULL ";
		}else{
			$SearchSQL.=" AND RDate IS NULL ";
		}
	}
	
	
	if(!empty($SFrom)&&!empty($STo)){
		$SearchSQL.=" AND (bookingDate>='".SwapDates($SFrom)."' AND bookingDate<='".SwapDates($STo)." 23:59:59') ";
	}
	if($_POST['properties']){
		if(is_array($_POST['properties'])){
			$SearchSQL.=" AND propertyID IN (";
			$First=true;
			foreach($_POST['properties'] as $val){
				if(!$First){
					$SearchSQL.=",";
					$Affs.=",";
				}
				$SearchSQL.=$val;
				$Affs.=$val;
				$First=false;
			}
			$SearchSQL.=") ";
		}
	}elseif(!empty($_GET['Affs'])){
		$_POST['properties']=split(",",$_GET['Affs']);
		$SearchSQL.=" AND propertyID IN (";
		$First=true;
		foreach($_POST['properties'] as $val){
			if(!$First){
				$SearchSQL.=",";
				$Affs.=",";
			}
			$SearchSQL.=$val;
			$Affs.=$val;
			$First=false;
		}
		$SearchSQL.=") ";
		$Affs=$_GET['Affs'];
	}
	
	$SQL1="SELECT COUNT(*) FROM bookings LEFT JOIN remittance ON (bookings.id=remittance.bookingsID),weeklysale,room WHERE bookings.weeklysaleID=weeklysale.id AND weeklysale.roomID=room.id $SearchSQL";
	//print $SQL1;
	$rset=$r->rawQuery($SQL1);
	$rdata=mysql_fetch_array($rset);
	$rcount=$rdata[0];
	$MaxPages=ceil($rcount/$RecordsPerPage);
	if($Page>$MaxPages) $Page=$MaxPages;
	$StartRecord=($Page-1)*$RecordsPerPage;
	if($StartRecord<0) $StartRecord=0;
	/*
	$SQL="SELECT DATE_FORMAT(bookingDate,'%a %e %b %Y'),DATE_FORMAT(checkin,'%a %e %b %Y'),domain,propertyName,affiliateCommission FROM bookings,affiliateDomains WHERE bookings.affiliateDomainsID=affiliateDomains.id AND  bookings.affiliateID='$_SESSION[AffKey]' $BSQL";
	*/
	$SQL2="SELECT DATE_FORMAT(bookingDate,'%a %e %b %Y'),confnum,guestName,(price*amount),bookings.id,DATE_FORMAT(RDate,'%e %b %Y'),propertyName FROM bookings LEFT JOIN remittance ON (bookings.id=remittance.bookingsID),weeklysale,room WHERE bookings.weeklysaleID=weeklysale.id AND weeklysale.roomID=room.id  $SearchSQL  ORDER BY propertyName,$OType LIMIT $StartRecord,$RecordsPerPage";
	$rset=$r->rawQuery($SQL2);
	
	//print $SQL2;
	$TargetUrl="remittance.php";
	$NPPage="Affs=$Affs&RType=$RType&SFrom=$SFrom&STo=$STo&SType=$SType&OType=$OType&SText=".urlencode($SText);
	$RecTo=($StartRecord+$RecordsPerPage);
	if($RecTo>$rcount) $RecTo=$rcount;

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Affiliate Bubble Administration</title>

<link href="<?=$rootdir?>css/admin.css" rel="stylesheet" type="text/css">
<script src="<?=$rootdir?>jscript/menuArray.js" language="javascript"></script>
<script src="<?=$rootdir?>jscript/clock.js" language="javascript"></script>
<script language="JavaScript" src="<?=$rootdir?>jscript/calendar1.js"></script><!-- Date only with year scrolling -->
</head>

<body onLoad="funClock();">
<?php include("../includes/header.php"); ?>
<table width="100%" border="0" cellspacing="25" cellpadding="0">
<tr>
<td valign="top" width="240"><div id="menu"><?php include("../menu.php"); ?></div></td>
<td align="center" valign="top">

<div id="title">
Remittance Report
</div>

<div id="search"> 
Enter search criteria.<br><br>
<form name="form1" method="post" action="remittance.php">
  <table width="271" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="29">From:</td>
      <td width="102"><input name="SFrom" type="text" class="formFields" id="SFrom" value="<?=($_POST['SFrom'] ? $_POST['SFrom'] :date("d-m-Y"));?>" size="10">
          <strong><a href="javascript:cal1.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
      <td width="23">To:</td>
      <td width="117"><input name="STo" type="text" class="formFields" id="STo" value="<?=($_POST['STo'] ? $_POST['STo'] :date("d-m-Y"));?>" size="10">
          <strong><a href="javascript:cal2.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
    </tr>
  </table>
  <br>
  <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="110">Search By: </td>
      <td><select name="RType" class="formFields">
          <option value="All" <?=($RType=="All" ? "selected" : "");?>>All Reservations</option>
          <option value="Paid" <?=($RType=="Paid" ? "selected" : "");?>>Status - Paid</option>
          <option value="Unpaid" <?=($RType=="Unpaid" ? "selected" : "");?>>Status - Unpaid</option>
      </select></td>
    </tr>
  </table>
  <br>
  <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="110">Included Properties:<br>
        (ctrl for multiple) </td>
      <td><select name="properties[]" size="7" multiple class="selectBox" id="properties[]">
          <?php
				$sq2=$r->rawQuery("SELECT id,propertyName FROM property ORDER BY propertyName");  
				while ($myrow = mysql_fetch_row($sq2)) {
					$tmp=(in_array($myrow[0],$_POST['properties']) ? "selected" : "");
					echo"<option value='$myrow[0]' $tmp>$myrow[1]</option>";
				};
			?>
            </select></td>
    </tr>
  </table>
  <br>
  <p align="right">
    <input type="submit" name="Submit" value="Search" class="formButtons">
  </p>
  <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#E9EEF5">
    <tr id="formHeader">
      <td><strong>Property</strong></td>
      <td><strong>Date</strong></td>
      <td align="center"><strong>Reservation Number </strong></td>
      <td align="center"><strong>Member Name</strong></td>
      <td align="center"><strong>Amount</strong></td>
      <td align="center"><strong>Options</strong></td>
      <td align="center"><strong>Payment Status</strong></td>
    </tr>
    <?php
$Count=0;
if($rcount>0){
	while ($myrow = mysql_fetch_row($rset)) {
		$Total+=$myrow[4];
?>
    <tr bgcolor="<?=(($Count%2)==0 ? "#FFFFFF" : "#EBEBEB"); ?>">
      <td bgcolor="#FFFFFF"><?=$myrow[6];?></td>
      <td bgcolor="#FFFFFF"><?=$myrow[0];?></td>
      <td align="center" bgcolor="#FFFFFF"><?=$myrow[1];?></td>
      <td align="center" bgcolor="#FFFFFF"><?=$myrow[2];?></td>
      <td align="center" bgcolor="#FFFFFF">$
          <?=$myrow[3];?></td>
      <td align="center" bgcolor="#FFFFFF"><a href="view-reservation.php?id=<?=$myrow[1];?>">View</a></td>
      <td align="center" bgcolor="#FFFFFF"><label>
        <?
			if($myrow[5]!=NULL){
				print "Paid - ".$myrow[5];
			}else{
		?>
        <input type="checkbox" name="bookingIDs[]" value="<?=$myrow[4];?>">
        <?
			};
		?>
      </label></td>
    </tr>
    <?
		};
	};
?>
    <tr>
      <td colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
      <td bgcolor="#FFFFFF">&nbsp;</td>
      <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
      <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
      <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
      <td align="center" bgcolor="#FFFFFF"><input name="Update" type="submit" id="Update" value="Update"></td>
    </tr>
  </table>
  <p align="right">&nbsp; </p>
</form>
</div>
<div id="title"> Property Name</div>
<div id="content"></div>

<div id="results">
  <p>
    <?=($StartRecord==0 ? ($RecTo==0 ? 0 : 1) : ($StartRecord+1));?>
&nbsp;-&nbsp;
<?=$RecTo;?>
&nbsp;of&nbsp;
<?=$rcount;?>
&nbsp;Results</p>
  <p>
    <? if($Page>1){ ?>
    <a href="<?=$TargetUrl;?>?Page=<?=$Page-1;?>&<?=$NPPage?>" >&lt;&lt;Back</a>
    <? }; ?>
    <?
		for($x=1;$x<=$MaxPages;$x++){
			?>
    <a href="<?=$TargetUrl;?>?Page=<?=$x;?>&<?=$NPPage?>" >
    <?=$x?>
    </a>
    <?
		};
	  ?>
    <?
						if($Page<$MaxPages){
					?>
    <a href="<?=$TargetUrl;?>?Page=<?=$Page+1;?>&<?=$NPPage?>" >Next &gt;&gt; </a>
    <?
						};
					?>
  </p>
  <p><a href="remittance-csv.php?<?=$NPPage;?>">EXPORT TO .CSV</a> </p>
</div>

</td>
</tr>
</table>
<script language="JavaScript">
<!-- // create calendar object(s) just after form tag closed
	 // specify form element as the only parameter (document.forms['formname'].elements['inputname']);
	 // note: you can have as many calendar objects as you need for your application
	var cal1 = new calendar1(document.forms['form1'].elements['SFrom']);
	cal1.year_scroll = true;
	cal1.time_comp = false;
	var cal2 = new calendar1(document.forms['form1'].elements['STo']);
	cal2.year_scroll = true;
	cal2.time_comp = false;
	
	

//-->
</script>
</body>
</html>
